<?php
defined('BME_INSERTION') or die('Error code: '.BME_INVALID_INSERT_ERROR);

require_once(BME_ROOT."gui/lcl/en.php");
require_once(BME_ROOT."gui/localize.php");

// Displaying
function BME_display_container() {
$BME_SITE = BME_SITE;
$BME_NAME = BME_NAME;
$BME_DESC = BME_DESC;
$BME_VERSION = BME_VERSION;
$BME_REQUEST = BME_get_request();
echo BME_localize(<<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
	<head>
		<script type='text/javascript' src='{$BME_SITE}lib/main.js'></script>
		<script type='text/javascript' src='{$BME_SITE}lib/ajaxer.js'></script>
		<script type='text/javascript' src='{$BME_SITE}lib/lrr.js'></script>
		<script type='text/javascript' src='{$BME_SITE}lib/justfold.js'></script>
		<link href='{$BME_SITE}lib/basic.css' rel='stylesheet' type='text/css'/>
		<link href='{$BME_SITE}lib/main.css' rel='stylesheet' type='text/css'/>
		<title>{$BME_NAME}</title>
	</head>
	<body onload='load("{$BME_SITE}","{$BME_REQUEST}");'>
		<div id='banner' onclick='BME.request("site/index")'>
			<h1><img alt='%%BME_NAME%%' src='{$BME_SITE}img/abord.png' style='border:0;margin:-45px -27px;'/> <span style='font-size:80%;font-weight:lighter;'>%%BME_DESC%%</span></h1>
			<div id='banner_menu'>
				<a style='background:url({$BME_SITE}img/home.png) no-repeat;'
					href='#site/index' onclick='BME.request("site/index")'>%%menu_home%%</a>
			</div>
		</div>
		<div id='foot_user'>&nbsp;</div>
		<div id='messages_container'>&nbsp;</div>
		<div id='global_container'>&nbsp;</div>
		<div style='text-align:right;color:gray;'>v$BME_VERSION</div>
	</body>
</html>
HTML
);
}
function html_a($trg,$txt,$tit="",$onclick="") {
	$onclick=htmlentities($onclick,ENT_QUOTES);
	return "<a href='javascript:void(null);' onclick='BME.request(\"$trg\");$onclick' title='$tit'>$txt</a>";
}


// Requests handling
function BME_get_request($eval=false) {
	if ( $req=$_REQUEST["request"] ) return $req;
	elseif ( $req=$_REQUEST["site"] ) BME_set_request("site/$req");
	elseif ( $req=$_REQUEST["js"] ) BME_set_request("js/$req");
	elseif ( $req=$_REQUEST["query"] ) BME_set_request("query/$req");
	elseif ( $req=$_REQUEST["task"] ) BME_set_request("task/$req");
	elseif ( $req=$_REQUEST["form"] ) BME_set_request("form/$req");
	elseif ( $req=$_REQUEST["admin"] ) BME_set_request("admin/$req");
	elseif ( $req=$_REQUEST["msg"] ) BME_set_request("msg/$req");
	else BME_set_request("site/index");
	return BME_get_request();
}
function BME_set_request($req) {
	assert('is_string($req)');
	
	$_REQUEST["request"]=$req;
	if ( BME_module_request($req) ) {
		$_REQUEST["module"] = BME_module_request($req);
		$_REQUEST[$_REQUEST["module"]] = substr($req,strlen($_REQUEST["module"])+1);
	} else {
		BME_set_request("$req/");
	}
	return;
}
function BME_parent_request($req) {
	assert('is_string($req)');
	
	$slash = strrpos($req,"/");
	if ( is_int($slash) ) {
		return substr($req,0,$slash);
	} else {
		return NULL;
	}
}
function BME_module_request($req) {
	assert('is_string($req)');
	
	$slash = strpos($req,"/");
	if ( is_int($slash) ) {
		return substr($req,0,$slash);
	}
	return null;
}
function BME_content_request($req) {
	assert('is_string($req)');
	
	if ( $mod = BME_module_request($req) ) {
		return substr($req,strlen($mod)+1);
	} else {
		return $req;
	}
}
if ( $_REQUEST["request"] ) BME_set_request($_REQUEST["request"]);
BME_get_request();

// Permissions
$BME_RESTRICTED_REQUESTS = array("admin","task","form","query");
$BME_RESTRICTION_EXCEPTIONS = array("form/example");
function BME_authentication_required($req=NULL) {
	global $BME_RESTRICTED_REQUESTS,$BME_RESTRICTION_EXCEPTIONS;
	if(is_null($req)) $req=BME_get_request();
	assert('is_string($req)');
	
	if(in_array($req, $BME_RESTRICTED_REQUESTS)){
		return true;
	} elseif (in_array($req,$BME_RESTRICTION_EXCEPTIONS)) {
		return false;
	} else {
		$par=BME_parent_request($req);
		if ($par) return BME_authentication_required($par);
		else return false;
	}
}


?>
